﻿using System;
using System.ComponentModel;
using System.Web.UI.WebControls;
using VioletDevelopmentLaboratory.Net.Web.Component;

namespace VioletDevelopmentLaboratory.Net.Web.WebControls
{
	public abstract class BoardWebControlBase : ComponentWebControlBase
	{
		#region Properties
		[Browsable(true),
		Category("Behavior")]
		public String ComponentID
		{
			get { return ViewState["ComponentID"].ToString(); }
			set { ViewState["ComponentID"] = value; }
		}

		[Browsable(true),
		Category("Design"),
		DefaultValue("default")]
		public String Template
		{
			get
			{
				return VUtility.GetControlSetting(ViewState["Template"] != null ? ViewState["Template"].ToString() : "", "Board.Template", "default");
			}
			set { ViewState["Template"] = value; }
		}

		[Browsable(true),
		Category("Design"),
		DefaultValue("default")]
		public String Stylesheet
		{
			get
			{
				return VUtility.GetControlSetting(ViewState["Stylesheet"] != null ? ViewState["Stylesheet"].ToString() : "", "Board.Stylesheet", "default");
			}
			set { ViewState["Stylesheet"] = value; }
		}
		#endregion

		private Board currentBoard;
		private VBoardUserInfo currentUserInfo;
		private Boolean currentUserFileDownload;

		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			this.currentBoard = Board.GetBoard(ComponentID);

			//알 수 없는 게시판
			if (currentBoard == null)
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board__InvalidComponentID"), "~/", true, 3);

			//템플릿 에러
			if (!System.IO.Directory.Exists(Server.MapPath(VUtility.WebApplicationVirtualPath + "vboard/component/board/template/" + Template)))
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board__InvalidTemplate"), "~/", true, 3);

			//스타일시트 에러
			if (!System.IO.Directory.Exists(Server.MapPath(VUtility.WebApplicationVirtualPath + "vboard/component/board/stylesheet/" + Stylesheet)))
				VUtility.GoToMessagePage(Page, VUtility.GetVWebGlobalResource("Board__InvalidStylesheet"), "~/", true, 3);

			#region Board.PageUrl 업데이트
			//String currentPage = Request.Path.ToLower(); //v0.1.3321.32733 버그번호2009020501
			String currentPage = Request.AppRelativeCurrentExecutionFilePath.ToLower();
			if (currentPage != currentBoard.PageUrl.ToLower())
				currentBoard.ModifyPageUrl(currentPage);
			#endregion

			#region 스타일시트 연결
			Literal styleLiteral = new Literal();
			styleLiteral.ID = "VWeb_Skin_StyleSheet";
			styleLiteral.Text = String.Format("<link rel=\"Stylesheet\" type=\"text/css\" href=\"{0}\" />", VUtility.WebApplicationVirtualPath + "vboard/component/board/stylesheet/" + Stylesheet + "/stylesheet.css");
			this.Page.Header.Controls.Add(styleLiteral);
			#endregion

			this.currentUserInfo = GetVBoardUserInfoBridge().GetUserInfo();

			this.currentUserFileDownload = currentUserInfo.IsAuthorizedBy(currentBoard, BoardCommand.FileDownload);

			#region BoardParameters.ClientPart에 따라 템플릿 로드
			switch (BoardParameters.Part)
			{
				//목록
				case BoardPart.List:
					this.loadListControl(true);
					break;
				//글보기
				case BoardPart.View:
					this.loadViewControl();
					break;
				//글쓰기
				case BoardPart.Form_Add:
					this.loadFormControl();
					break;
				//글변경
				case BoardPart.Form_Modify:
					this.loadFormControl();
					break;
			}
			#endregion
		}

		private void loadInformationControl()
		{
			BoardInformationPartBase partControl = (BoardInformationPartBase)this.LoadControl(VUtility.WebApplicationVirtualPath + "vboard/component/board/template/" + Template + "/information.ascx");

			if (partControl.Page.Items["CurrentBoard"] == null)
				partControl.Page.Items["CurrentBoard"] = currentBoard;

			if (partControl.Page.Items["CurrentUserInfo"] == null)
				partControl.Page.Items["CurrentUserInfo"] = currentUserInfo;

			if (partControl.Page.Items["CurrentUserFileDownload"] == null)
				partControl.Page.Items["CurrentUserFileDownload"] = currentUserFileDownload;

			partControl.Page.Items["Template"] = Template;
			partControl.Page.Items["Stylesheet"] = Stylesheet;

			TemplatePlaceHolder.Controls.Add(partControl);
		}

		private void loadListControl(Boolean addInformation)
		{
			if (addInformation)
				loadInformationControl();

			BoardCategoryCollection categories;
			BoardThreadCollection threads;

			currentBoard.GetListPartData(BoardParameters.CategoryID, Server.UrlDecode(BoardParameters.SearchText), BoardParameters.PageIndex,
				out categories, out threads);

			BoardListPartBase partControl = (BoardListPartBase)this.LoadControl(VUtility.WebApplicationVirtualPath + "vboard/component/board/template/" + Template + "/list.ascx");

			if (partControl.Page.Items["CurrentBoard"] == null)
				partControl.Page.Items["CurrentBoard"] = currentBoard;

			if (partControl.Page.Items["CurrentUserInfo"] == null)
				partControl.Page.Items["CurrentUserInfo"] = currentUserInfo;

			if (partControl.Page.Items["CurrentUserFileDownload"] == null)
				partControl.Page.Items["CurrentUserFileDownload"] = currentUserFileDownload;


			partControl.Page.Items.Add("CurrentCategoryCollection", categories != null ? categories : new BoardCategoryCollection());
			partControl.Page.Items.Add("CurrentThreadCollection", threads);

			TemplatePlaceHolder.Controls.Add(partControl);
		}

		private void loadViewControl()
		{
			this.loadInformationControl();

			//데이터 로드
			BoardThread thread;
			BoardThreadCollection groupThreads;
			AttachmentFileInfoCollection threadFiles;
			BoardCommentCollection comments;
			Vote vote;
			TrackbackCollection trackbacks;

			currentBoard.GetViewPartData(BoardParameters.ThreadID, out thread, out groupThreads,
				out threadFiles, out comments, out vote, out trackbacks);


			BoardViewPartBase partControl = (BoardViewPartBase)this.LoadControl(VUtility.WebApplicationVirtualPath + "vboard/component/board/template/" + Template + "/view.ascx");

			if (partControl.Page.Items["CurrentBoard"] == null)
				partControl.Page.Items["CurrentBoard"] = currentBoard;

			if (partControl.Page.Items["CurrentUserInfo"] == null)
				partControl.Page.Items["CurrentUserInfo"] = currentUserInfo;

			if (partControl.Page.Items["CurrentUserFileDownload"] == null)
				partControl.Page.Items["CurrentUserFileDownload"] = currentUserFileDownload;

			partControl.Page.Items.Add("CurrentThread", thread);
			partControl.Page.Items.Add("CurrentGroupThreadCollection", groupThreads);
			partControl.Page.Items.Add("CurrentAttachmentFileInfoCollection", threadFiles);
			partControl.Page.Items.Add("CurrentCommentCollection", comments);
			partControl.Page.Items.Add("CurrentVote", vote);
			partControl.Page.Items.Add("CurrentTrackbackCollection", trackbacks);

			TemplatePlaceHolder.Controls.Add(partControl);

			if (currentBoard.UseListView)
				loadListControl(false);
		}

		private void loadFormControl()
		{
			this.loadInformationControl();

			BoardCategoryCollection categories;
			BoardThread thread;
			AttachmentFileInfoCollection threadFiles;
			Vote vote;

			currentBoard.GetFormPartData(BoardParameters.ThreadID, out categories, out thread, out threadFiles, out vote);

			BoardFormPartBase partControl = (BoardFormPartBase)this.LoadControl(VUtility.WebApplicationVirtualPath + "vboard/component/board/template/" + Template + "/form.ascx");

			if (partControl.Page.Items["CurrentBoard"] == null)
				partControl.Page.Items["CurrentBoard"] = currentBoard;

			if (partControl.Page.Items["CurrentUserInfo"] == null)
				partControl.Page.Items["CurrentUserInfo"] = currentUserInfo;

			if (partControl.Page.Items["CurrentUserFileDownload"] == null)
				partControl.Page.Items["CurrentUserFileDownload"] = currentUserFileDownload;

			partControl.Page.Items.Add("CurrentCategoryCollection", categories != null ? categories : new BoardCategoryCollection());
			partControl.Page.Items.Add("CurrentThread", thread);
			partControl.Page.Items.Add("CurrentAttachmentFileInfoCollection", threadFiles);
			partControl.Page.Items.Add("CurrentVote", vote);

			TemplatePlaceHolder.Controls.Add(partControl);
		}
	}
}
